package com.njsf.controller.front;

<<<<<<< HEAD
import com.njsf.pojo.OrderDetailPlus;
import com.njsf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

=======
import com.njsf.dao.OrderMapper;
import com.njsf.dto.ResponseJson;
import com.njsf.pojo.Order;
import com.njsf.pojo.OrderDetailPlus;
import com.njsf.pojo.OrderInfoVO;
import com.njsf.pojo.User;
import com.njsf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/front")
public class OrderFrontCotroller {

    @Autowired
    private OrderService orderService;
<<<<<<< HEAD

    @RequestMapping("/orderinfo")
    public String orderInfo(String orderNumber,Map<String,Object> map){
        List<OrderDetailPlus> orderDetais = orderService.findOrderDetais(orderNumber);
        map.put("orderDetais",orderDetais);
        return "front/orderinfo";
    }

=======
    @Autowired
    private OrderMapper orderMapper;

    @RequestMapping("/orderinfo")
    public String orderInfo(String orderNumber, Map<String, Object> map) {
        List<OrderDetailPlus> orderDetais = orderService.findOrderDetais(orderNumber);
        map.put("orderDetais", orderDetais);
        return "front/orderinfo";
    }

    /**
     * 点击支付订单后会走到这
     *
     * @param phone
     * @param address
     * @param orderNumber
     * @return
     */
    @RequestMapping("/payfororder")
    public String payForOrder(String phone, String address, String orderNumber, RedirectAttributes redirectAttributes) {
        ResponseJson responseJson = orderService.updateUserInfo(phone, address, orderNumber);
        // 执行支付功能
        Order order = orderMapper.findByOrderNumber(orderNumber);
        Map<String, String> orderInfo = new HashMap<>();
        orderInfo.put("WIDout_trade_no", orderNumber);
        orderInfo.put("WIDtotal_amount", order.getTotalMoney() + "");
        orderInfo.put("WIDsubject", "测试支付");
        orderInfo.put("WIDbody", "测试商品");
        redirectAttributes.addFlashAttribute("orderInfo", orderInfo);
        return "redirect:/pay/alipay";
    }

    @RequestMapping("/orderlist")
    public String orderList(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/front/loginview";
        }
        List<String> orderNumber = orderMapper.findOrderNumberByUid(user.getId());
        Map<String, List<OrderInfoVO>> orderInfo = new HashMap<>();
        Map<String, Double> orderTotalPrice = new HashMap<>();

        for (String oid : orderNumber) {
            List<OrderInfoVO> orderInfoByOrderNum = orderMapper.findOrderInfoByOrderNum(oid);
            double price = 0d;
            for (OrderInfoVO orderInfoVO : orderInfoByOrderNum) {
                price += orderInfoVO == null ? 0 : orderInfoVO.getPrice() == null ? 0 : orderInfoVO.getPrice();
            }
            orderTotalPrice.put(oid, price);
            orderInfo.put(oid, orderInfoByOrderNum);
        }
        // 向request中存储数据2
        model.addAttribute("totalPrice", orderTotalPrice);
        model.addAttribute("orderNum", orderNumber);
        model.addAttribute("orderInfo", orderInfo);
        return "front/orderlist";
    }

>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
}
